package com.oa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.oa.back.ResponseBack;
import com.oa.common.base.Page;
import com.oa.common.base.Pageable;
import com.oa.common.dto.AppsDto;
import com.oa.common.dto.TransectDto;
import com.oa.common.enums.CodeStatus;
import com.oa.common.enums.PersonType;
import com.oa.common.enums.apps.FromFileType;
import com.oa.common.utils.RandomUnit;
import com.oa.dao.AppsDao;
import com.oa.dao.BaseDao;
import com.oa.dao.JdbcDao;
import com.oa.domain.AppsData;
import com.oa.entities.User;
import com.oa.entities.flows.AppType;
import com.oa.entities.flows.Apps;
import com.oa.entities.flows.Flow;
import com.oa.entities.flows.Form;
import com.oa.service.*;
import com.oa.untils.TplUntils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class AppsServiceImpl extends UndeleteServiceImpl<Long,Apps> implements AppsService {

    @Autowired
    private AppsDao appsDao;

    @Autowired
    private UserService userService;

    @Autowired
    private JdbcDao jdbcDao;

    @Autowired
    private AppsTypeService appsTypeService;

    @Autowired
    private PersonnelService personnelService;

    @Autowired
    private FormService formService;

    @Autowired
    private TplHtmlService tplHtmlService;

    @Override
    protected BaseDao<Long, Apps> getSelfDao() {
        return appsDao;
    }


    @Override
    public void setClass(Class<Apps> clazz) {

    }

    @Override
    @Transactional
    public ResponseBack<Long> submit(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        AppsDto dto= JSONObject.parseObject(params,AppsDto.class);
        if (dto==null){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        AppType type=appsTypeService.get(dto.getAppId());
        if (type==null){
            return ResponseBack.fial(CodeStatus.APPSTYPE_UNDERFUND.getResponseStatus());
        }
        if (dto.getType()==null){
            dto.setType(0);
        }
        Apps apps= new Apps();
        apps.setAppsType(PersonType.UNAUDITED.getCode());
        apps.setType(dto.getType());
        apps.setAppType(type);
        apps.setName(dto.getName());
        apps.setTitle(type.getTitle());
        apps.setNumber(Long.valueOf(System.currentTimeMillis()+RandomUnit.getRandom()));
        User user= userService.getCurrent();
        apps.setIssue(user);
        apps.setType(0);
        apps.setIssueTime(System.currentTimeMillis());
        save(apps);
        personnelService.save(apps,dto.getAppId(),1,null);
        AppsData data= new AppsData();
        data.setAppid(apps.getId());
        data.setTplid(dto.getAppId());
        data.setTypetype(dto.getType());
        jdbcDao.update(data);
        return ResponseBack.success(apps.getId());
    }

    @Override
    @Transactional
    public Page<Apps> findPage(Pageable pageable, String key, Integer type) {
        return appsDao.findPage(pageable,key,type,userService.getCurrent().getId());
    }

    @Override
    @Transactional
    public ResponseBack<String> transect(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        TransectDto dto= JSONObject.parseObject(params,TransectDto.class);
        if (dto==null){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        Apps apps= get(dto.getAppsId());
        AppType type= appsTypeService.get(dto.getTypeId());
        if (apps==null){
            return ResponseBack.fial(CodeStatus.APPS_UNDERFUND.getResponseStatus());
        }
        if (type==null){
            return ResponseBack.fial(CodeStatus.APPSTYPE_UNDERFUND.getResponseStatus());
        }
        Map<String,Object> map= dto.getMap();
        jdbcDao.update(dto.getTypeId(),map,dto.getAppsId());
        return ResponseBack.success(null);
    }

    @Override
    @Transactional
    public String getTemplate(Long typeId, Long appId) {
        String views= tplHtmlService.getViews(typeId);
        Apps apps=get(appId);
        Flow flow= apps.getPersonnel().getFlow();
        List<String> strings=flow.getForms();
        List<Form> forms= formService.findForm(typeId);
        Map<String,Object> map= jdbcDao.queryObject(typeId,appId);
        for (Form form:forms){
            String inputName= form.getInputName();
            String value= null;
            if (map.get(inputName)!=null){
                value= map.get(inputName).toString();
            }
            if (strings.contains(form.getInputName())){

                views=warm(form,value,false,views);
            }else {
                views=warm(form,value,true,views);
            }
        }
        return views;
    }

    @Override
    public String getViews(Long typeId, Long appsId) {
        String views= tplHtmlService.getViews(typeId);
        List<Form> forms= formService.findForm(typeId);
        Map<String,Object> map= jdbcDao.queryObject(typeId,appsId);
        for (Form form:forms){
            String value= "";
            if (map.get(form.getInputName())!=null){
                value= map.get(form.getInputName()).toString();
            }
           views= view(form.getInputName(),value,views);
        }
        return views;
    }

    @Override
    @Transactional
    public ResponseBack<String> core(Long appsId, Long personId,int appsType) {
        Apps apps= get(appsId);
        if (apps==null){
            return ResponseBack.fial(CodeStatus.APPS_UNDERFUND.getResponseStatus());
        }
        apps.setAppsType(appsType);
        apps.setApprovaldate(System.currentTimeMillis());
        update(apps);
        Apps newApps= new Apps();
        newApps.setAppsType(PersonType.UNAUDITED.getCode());
        newApps.setIssueTime(System.currentTimeMillis());
        newApps.setType(apps.getType());
        newApps.setNumber(new Long(RandomUnit.getRandom()+System.currentTimeMillis()));
        User user= userService.getCurrent();
        newApps.setIssue(user);
        newApps.setAppType(apps.getAppType());
        newApps.setTitle(apps.getTitle());
        newApps.setName(apps.getName());
        save(newApps);
        personnelService.save(newApps,newApps.getAppType().getId(),apps.getPersonnel().getFlow().getNum()+1,personId);
        AppsData appsData= new AppsData();
        appsData.setAppid(newApps.getId());
        appsData.setTplid(newApps.getAppType().getId());
        appsData.setTypetype(newApps.getType());
        jdbcDao.update(appsData);
        return ResponseBack.success(null);
    }

    private String view(String inputName,String value,String views){
        views=views.replace("<!--{"+inputName+"}-->",value);
        return views;
    }

    /**
     * 将表单放入文件中
     *
     * @param form
     * 表单
     *
     * @param value
     * 值
     *
     * @param isDisabled
     * 是否禁用
     *
     * @param views
     * 读到的文件
     *
     * @return
     */
    private String warm(Form form,String value,Boolean isDisabled,String views){
        String input=null;
        if (form.getFileType()==FromFileType.CANVAS.getCode()){
            input= TplUntils.appButton(form.getInputName(),isDisabled,value);
        }else if (form.getFileType()==FromFileType.SELECT.getCode()){
            input=TplUntils.appsSelect(form.getInputName(),form.getOptiones(),value,new Integer(form.getW())
                    ,new Integer(form.getH()),isDisabled);
        }else if (form.getFileType()== FromFileType.INPUT.getCode()){
            input=TplUntils.appsInput(form.getInputName(),new Integer(form.getW()),
                    new Integer(form.getH()),value,isDisabled);
        }else  if (form.getFileType()==FromFileType.TEXTAREA.getCode()){
            input= TplUntils.appsTextarea(form.getInputName(),new Integer(form.getW()),new Integer(form.getH()),value,isDisabled);
        }else if (form.getFileType()== FromFileType.CHECKBOX.getCode()){
            input= TplUntils.appsCheckbox(form.getInputName(),form.getOptiones(),value,isDisabled);
        }else if (form.getFileType()==FromFileType.RADIO.getCode()){
            input= TplUntils.appsRadio(form.getInputName(),form.getOptiones(),value,isDisabled);
        }
        if (!StringUtils.isEmpty(input)){
            views=views.replace("<!--{"+form.getInputName()+"}-->",input);
        }
        return views;
    }
}
